package com.yc.sole.system.biz;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.yc.sole.framework.mybatis.page.Paging;
import com.yc.sole.framework.mybatis.util.PageUtil;
import com.yc.sole.system.dto.BizLogDto;
import com.yc.sole.system.dto.UserDto;
import com.yc.sole.system.repository.BizLogRepository;
import com.yc.sole.system.repository.UserRepository;
import com.yc.sole.system.request.BizLogQueryReq;
import com.yc.sole.system.request.BizLogReq;
import com.yc.sole.system.response.BizLogResp;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 服务接口实现
 *
 * @author yizuomin
 * @date 2024-03-20
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class BizLogBiz {

    private final BizLogRepository bizLogRepository;
    private final UserRepository userRepository;

    /**
     * 新增
     *
     * @param bizLogReq
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveBizLog(BizLogReq bizLogReq) {
        BizLogDto dto = new BizLogDto();
        BeanUtil.copyProperties(bizLogReq, dto);
        bizLogRepository.saveBizLog(dto);
    }

    /**
     * 修改
     *
     * @param bizLogReq
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateBizLogById(BizLogReq bizLogReq) {
        BizLogDto dto = new BizLogDto();
        BeanUtil.copyProperties(bizLogReq, dto);
        bizLogRepository.updateBizLogById(dto);
    }

    /**
     * 通过id批量删除
     *
     * @param ids
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteBizLogByIds(Set<Long> ids) {
        bizLogRepository.deleteBizLogByIds(ids);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    public BizLogResp selectBizLogById(Long id) {
        return BeanUtil.copyProperties(bizLogRepository.selectBizLogById(id), BizLogResp.class);
    }

    /**
     * 分页查询
     *
     * @param page
     * @param queryReq
     * @return
     */
    public Paging<BizLogResp> selectBizLogPage(Paging<BizLogQueryReq> page, BizLogQueryReq queryReq) {
        Paging<BizLogDto> paging = bizLogRepository.selectBizLogPage(page, queryReq);
        return PageUtil.pageCopy(paging, BizLogResp.class);
    }

    /**
     * @param basePageReq
     * @return
     */
    public List<BizLogResp> selectBizLogByBizId(BizLogQueryReq basePageReq) {
        List<BizLogDto> bizLogDtoList = bizLogRepository.selectBizLogByTableNameAndBizId(basePageReq.getTableName(), basePageReq.getBizId());
        List<BizLogResp> bizLogRespList = BeanUtil.copyToList(bizLogDtoList, BizLogResp.class);
        if (CollUtil.isEmpty(bizLogRespList)) {
            return bizLogRespList;
        }
        Set<Long> userIds = bizLogRespList.stream().map(BizLogResp::getUserId).collect(Collectors.toSet());
        Map<Long, UserDto> userDtoMap = userRepository.selectUserByIds(userIds).stream().collect(Collectors.toMap(UserDto::getId, v -> v));
        for (BizLogResp bizLogResp : bizLogRespList) {
            UserDto userDto = userDtoMap.get(bizLogResp.getUserId());
            if (Objects.nonNull(userDto)) {
                bizLogResp.setUsername(userDto.getUsername());
            }
        }
        return bizLogRespList;
    }
}
